home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / zcpp_jae.zip / GENERATE.C < prev    next >
C/C++ Source or Header  |  1990-07-06  |  3KB  |  108 lines

  1. /*
  2.  
  3.  
  4.  Copyright (C) 1990 Texas Instruments Incorporated.
  5.  
  6.  Permission is granted to any individual or institution to use, copy, modify,
  7.  and distribute this software, provided that this complete copyright and
  8.  permission notice is maintained, intact, in all copies and supporting
  9.  documentation.
  10.  
  11.  Texas Instruments Incorporated provides this software "as is" without
  12.  express or implied warranty.
  13.  
  14.  
  15.  *
  16.  * Edit history
  17.  * Created: LGO 30-Mar-89 -- Initial design and implementation.
  18.  *
  19.  * GENERATE(arg,things)s{body}
  20.  *
  21.  * where:
  22.  *   "things" is a comma-separated list of arguments
  23.  *   "body"   is expanded once for each thing in "things"
  24.  *   "arg"    is substituted for a "thing" while expanding "body"
  25.  *   "s"      is placed after each "body" expansion, except for the last time.
  26.  *          useful values of "s" are nothing, a space, or a comma.
  27.  *
  28.  * The result of expanding GENERATE is the result of expanding all the "body"
  29.  * expansions with all newline's compressed out.
  30.  *
  31.  * Example:
  32.  *
  33.  * MACRO ODD(REST: ints)
  34.  * { GENERATE(x, ints),{
  35.  *    #if x&1
  36.  *      x
  37.  *    #endif
  38.  *   }
  39.  * }
  40.  *
  41.  * ODD(1,2,3,4,5,6) // expands to     1,     3,     5
  42.  *
  43.  */
  44.  
  45. #define MAX_ARGS 512
  46.  
  47. #include "defmacio.h"
  48. #include "macro.h"
  49.  
  50. int generate(argc, argv)
  51.      int argc;
  52.      char* argv[];
  53. {
  54.   char c;
  55.   int nargs = 0;
  56.   int i;
  57.   char* subst[1];
  58.   char* value[1];
  59.   char* args[MAX_ARGS];
  60.   char seperator = EOS;
  61.   char* body;
  62.   char* bp;
  63.   scan_next(' ');              /* Skip macro name */
  64.   c = skip_blanks();
  65.   if(c != '(') {
  66.     fprintf(stderr, "GENERATE: Can't find argument list\n");
  67.     return 1;
  68.   }
  69.   for (;;) {
  70.     if(nargs >= MAX_ARGS) {
  71.       fprintf(stderr, "GENERATE: more than %d arguments\n", MAX_ARGS);
  72.       return 1;
  73.     }
  74.     args[nargs++] = scan_token(",)");
  75.     c = getchar();
  76.     if (c == ')') break;
  77.     if (c == EOF) {
  78.       fprintf(stderr, "GENERATE: unexpected end of file\n");
  79.       return 1;
  80.     }
  81.   }
  82.   c = skip_blanks();
  83.   if (c != '{') {
  84.     seperator = c;
  85.     c = skip_blanks();
  86.   }
  87.   if (c != '{') {
  88.     fprintf(stderr, "GENERATE: Body missing, found %c instead\n", c);
  89.     return 1;
  90.   }
  91.   unget();
  92.   body = scan_next('}');          /* Grab the macro body */
  93.   while(*body++ != '{');          /* strip start of body */
  94.   bp = body + strlen(body);          /* Strip end of body */
  95.   *--bp = EOS;
  96.  
  97.   bp = work_string->buff;          /* First parm is subst */
  98.   subst[0] = args[0];
  99.   bp += strlen(subst[0]) + 1;
  100.   for (i=1; i<nargs; i++) {
  101.     value[0] = args[i];
  102.     macro_substitute(body, 1, subst, value);
  103.     free(args[i]);
  104.     if (seperator != EOS && i+1 < nargs) putchar(seperator);
  105.   }
  106.   return 0;
  107. }
  108.